//
//  AssetsRecordViewController.m
//  BTCoin
//
//  Created by Robin on 27/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "AssetsRecordViewController.h"
#import "AssetRecordTableCell.h"
#import "TradeService.h"
#import "AssetsRecordModel.h"
#import "NSDate+convenience.h"
#import "NSDate+Util.h"

@interface AssetsRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UIScrollView * scrollView;
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) UILabel * emptyLabel;
@property(nonatomic,strong) AssetsRecordModel * recordModel;
@end

@implementation AssetsRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleText:@"资产记录"];
    [self initUIHead];
    [self requestAssertRecords];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUIHead {
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = UIColorFromRGB(0xEFEFEF);
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.top.equalTo(self.headView.mas_bottom);
    }];
    
    UIView * scrollContainerView = [[UIView alloc] init];
    [self.scrollView addSubview:scrollContainerView];
    [scrollContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.scrollView);
        make.bottom.equalTo(self.view);
    }];
    
    UIView * titleContainerView = [[UIView alloc] init];
    titleContainerView.backgroundColor = [UIColor whiteColor];
    [scrollContainerView addSubview:titleContainerView];
    [titleContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.scrollView);
        make.height.mas_equalTo(40.0);
    }];
    
    NSArray * titleArray = @[@"日期",@"类型",@"人民币(￥)",@"比特币(฿)",@"莱特币(L)",@"以太币(E)",@"以太经典(E)"];
    for (int i = 0; i < titleArray.count; i++) {
        UILabel *label = [UILabel new];
        label.backgroundColor = [UIColor clearColor];
        label.text = titleArray[i];
        label.tag = i+1;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        [titleContainerView addSubview:label];
        
        if (0 == i) {
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.centerY.equalTo(titleContainerView);
                make.width.mas_equalTo(90.0);
            }];
        } else if (titleArray.count == i+1) {
            UILabel * pLabel = (UILabel *)[titleContainerView viewWithTag:i];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(pLabel.mas_trailing);
                make.centerY.trailing.equalTo(titleContainerView);
                make.width.equalTo(pLabel);
            }];
        } else {
            UILabel * pLabel = (UILabel *)[titleContainerView viewWithTag:i];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.equalTo(pLabel.mas_trailing).offset(5);
                make.centerY.equalTo(titleContainerView);
                make.width.equalTo(pLabel);
            }];
        }
    }
    
    self.emptyLabel = [[UILabel alloc] init];
    self.emptyLabel.textColor = color_333333;
    self.emptyLabel.text = @"暂无相关数据";
    self.emptyLabel.font = [UIFont systemFontOfSize:17.0];
    [scrollContainerView addSubview:self.emptyLabel];
    [self.emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(scrollContainerView);
    }];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"AssetRecordTableCell" bundle:nil] forCellReuseIdentifier:@"AssetRecordTableCell"];
    [scrollContainerView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(scrollContainerView);
        make.top.equalTo(titleContainerView.mas_bottom);
    }];
    [self.tableView reloadData];
}

- (void)requestAssertRecords {
    Loading_Pig_Show(self.view);
    [[TradeService sharedTradeService] assetsrecord:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.recordModel = [[AssetsRecordModel alloc] initWithDictionary:responseObject error:nil];
            [self updateUI];
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

- (void)updateUI {
    self.tableView.hidden = 0 == self.recordModel.list.count;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.recordModel.list.count*2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ChannelIdentifier = @"AssetRecordTableCell";
    AssetRecordTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ChannelIdentifier forIndexPath:indexPath];
    
    [self configCell:cell indexPath:indexPath];
    
    return cell;
}

- (void)configCell:(AssetRecordTableCell *)cell indexPath:(NSIndexPath *)indexPath {
    ARecordDetailModel * detailModel = self.recordModel.list[indexPath.row/2];
    NSDate * date = [[NSDate alloc] initWithTimeIntervalSince1970:detailModel.fcreatetime.time/1000.0];
    cell.dateLabel.text = [date stringWithDateFormate:@"yyyy-MM-dd"];
    if (0 == indexPath.row%2) {
        cell.typeLabel.text = @"可用";
        cell.CNYLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"0"][@"total"]];
        cell.BTCLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"1"][@"total"]];
        cell.LiteLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"12"][@"total"]];
        cell.EtherLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"13"][@"total"]];
        cell.EtherClassicLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"14"][@"total"]];
    } else {
        cell.typeLabel.text = @"冻结";
        cell.CNYLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"0"][@"frozen"]];
        cell.BTCLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"1"][@"frozen"]];
        cell.LiteLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"12"][@"frozen"]];
        cell.EtherLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"13"][@"frozen"]];
        cell.EtherClassicLabel.text = [NSString stringWithFormat:@"%@",detailModel.detail[@"14"][@"frozen"]];
    }
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40.0;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
